﻿using System;
using System.Data;
using System.Collections.Generic;
using System.IO;

namespace LDSP.BRL
{
    public partial class ExposureDraft : BYFW.BOL.BaseEntity
    {
        public ExposureDraft()
        {
            m_tableName = "t_exposureDraft";
            m_hasName = true;
            m_tblCacheManager.IsEnable = true;
        }

        protected string m_organization = string.Empty;
        protected DateTime m_releaseDT = DateTime.MinValue;
        protected DateTime m_endDT = DateTime.MinValue;
        protected DateTime m_uploadDT = DateTime.MinValue;
        protected string m_summary = string.Empty;
        protected string m_contact = string.Empty;
        protected string m_address = string.Empty;
        protected string m_phone = string.Empty;
        protected string m_email = string.Empty;
        protected string m_fileName = string.Empty;
        protected string m_fileExtension = string.Empty;
        protected string m_comment = string.Empty;
        protected string m_reference_number = string.Empty;
        protected bool m_isactive = true;

        /// <summary>
        /// 发布机构
        /// </summary>
        public string Organization
        {
            get { return m_organization; }
            set { m_organization = value; }
        }
        /// <summary>
        /// 发布文号
        /// </summary>
        public string ReferenceNumber
        {
            get { return m_reference_number; }
            set { m_reference_number = value; }
        }
        /// <summary>
        /// 发布日期
        /// </summary>
        public DateTime ReleaseDT
        {
            get { return m_releaseDT; }
            set { m_releaseDT = value; }
        }
        /// <summary>
        /// 截止日期
        /// </summary>
        public DateTime EndDT
        {
            get { return m_endDT; }
            set { m_endDT = value; }
        }
        /// <summary>
        /// 上传日期
        /// </summary>
        public DateTime UploadDT
        {
            get { return m_uploadDT; }
            set { m_uploadDT = value; }
        }
        /// <summary>
        /// 概述
        /// </summary>
        public string Summary
        {
            get { return m_summary; }
            set { m_summary = value; }
        }
        /// <summary>
        /// 联系人
        /// </summary>
        public string Contact
        {
            get { return m_contact; }
            set { m_contact = value; }
        }
        /// <summary>
        /// 地址
        /// </summary>
        public string Address
        {
            get { return m_address; }
            set { m_address = value; }
        }
        /// <summary>
        /// 电话
        /// </summary>
        public string Phone
        {
            get { return m_phone; }
            set { m_phone = value; }
        }
        /// <summary>
        /// 邮件
        /// </summary>
        public string Email
        {
            get { return m_email; }
            set { m_email = value; }
        }
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName
        {
            get { return m_fileName; }
            set { m_fileName = value; }
        }
        /// <summary>
        /// 文件扩展名
        /// </summary>
        public string FileExtension
        {
            get { return m_fileExtension; }
            set { m_fileExtension = value; }
        }

        /// <summary>
        /// 备注
        /// </summary>
        public string Comment
        {
            get
            {
                return m_comment;
            }
            set
            { m_comment = value; }
        }

        /// <summary>
        /// 是否过期
        /// </summary>
        public bool IsActive
        {
            get
            {
                return m_isactive;
            }
            set
            {
                m_isactive = value;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("organization", m_organization);
            m_container.Add("referenceNumber", m_reference_number);
            m_container.Add("releaseDT", m_releaseDT);
            m_container.Add("endDT", m_endDT);
            m_container.Add("uploadDT", m_uploadDT);
            m_container.Add("summary", m_summary);
            m_container.Add("contact", m_contact);
            m_container.Add("address", m_address);
            m_container.Add("phone", m_phone);
            m_container.Add("email", m_email);
            m_container.Add("fileName", m_fileName);
            m_container.Add("fileExtension", m_fileExtension);
            m_container.Add("comment", m_comment);
            m_container.Add("isactive", m_isactive);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_organization = this.GetStringValue(record, "organization");
            m_reference_number = this.GetStringValue(record, "referenceNumber");
            m_releaseDT = this.GetDateValue(record, "releaseDT");
            m_endDT = this.GetDateValue(record, "endDT");
            m_uploadDT = this.GetDateValue(record, "uploadDT");
            m_summary = this.GetStringValue(record, "summary");
            m_contact = this.GetStringValue(record, "contact");
            m_address = this.GetStringValue(record, "address");
            m_phone = this.GetStringValue(record, "phone");
            m_email = this.GetStringValue(record, "email");
            m_fileName = this.GetStringValue(record, "fileName");
            m_fileExtension = this.GetStringValue(record, "fileExtension");
            m_comment = this.GetStringValue(record, "comment");
            m_isactive = this.GetBoolValue(record, "isactive");
            return true;
        }

        /// <summary>
        /// Delete the file if it exists
        /// </summary>
        private void DeleteFile()
        {
            string fileFullPath = this.GetDownloadFileFullName();
            if (File.Exists(fileFullPath))
            {
                File.Delete(fileFullPath);
            }
        }

        public string GetDownloadFileFullName()
        {
            try
            {
                return Path.Combine(LDSP.Util.CommonService.SITEROOT, "Docs", FileExtension);
            }
            catch { return ""; }
        }

        /// <summary>
        /// 根据filename创建fileextension
        /// </summary>
        public void CreateNewFile(string fileName)
        {
            this.DeleteFile();
            m_fileName = fileName;
            string ext = Path.GetExtension(m_fileName);
            m_fileExtension = Guid.NewGuid().ToString() + ext;
        }
    }
}